06. Exercise: The Application Class and Timber

ANDK L4 09 The Application Class And Timber SC

Now it’s your turn to complete this exercise yourself.

In this exercise you'll add logging for the most important lifecycle callbacks, using the logging library Timber.

**1. Add Timber library to gradle file (Module: app): **

You can find latest Timber dependency version here. Open up your Module app build.gradle file and add to the dependencies {} block.

**2. Make an Application class: **

class PusherApplication : Application() {

   override fun onCreate() {
       super.onCreate()
   }
}

**3. Add the application class to the manifest: **

Make sure you add it to the <application> tag.

android:name=".PusherApplication"

**4. Initialize Timber in the application class: **

Installation of logging trees should be done as early as possible. The onCreate of your application is the most logical choice. Go ahead, locate the onCreate callback in your application class and plant Timber tree there.

Timber.plant(Timber.DebugTree())

**5. Update log statements from previous exercise to timber logs: **

These log statements are in onCreate and onStart. Change them to Timber log statements which look like this for info level log statements:

Timber.i("message here")

**6. Override the rest of the lifecycle methods and use Timber to print log statements for all: **

Here are the callback methods to override:

  • onResume
  • onPause
  • onStart
  • onDestroy
  • onRestart
  • onStop

Remember you can use the keyboard shortcut Ctrl + O for overriding methods.

If you want to start at this step, you can download this exercise code from: Step.02-Exercise-Timber-for-logging.

You will find plenty of // TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.

Once you’re done, you can check your solution against the solution we’ve provided here Step.02-Solution-Timber-for-logging or using this git diff

Task Description:

Check the steps below as you implement them to complete this exercise.

Task List:

Task Feedback:

Well done!

Solution: Step.02-Solution-Timber-for-logging or using this git diff